// /////////////////////////////////////////////////////////////////////////////
//
// Name:            asDataStack.cpp
// Author:          Michael Bartsch (ZCCdark203)
//
// Desc :           Binds the DataStack class to AngelScript
//
// License:         Copyright (C) 2009 Michael Bartsch and Contributors
//
//                  This program is free software: you can redistribute it
//                  and/or modify it under the terms of the zlib/libpng License.
//                  See main.cpp for conditions of distribution and use.
//
// /////////////////////////////////////////////////////////////////////////////

// Include files
#include "asDataStack.h"

#ifdef __COMPILE_WITH_ANGELSCRIPT__

#include "../ScriptHelper.h"

#include "../../core/DataStack.h"


//! Reference factory for the DataStack class.
DataStack* createDataStack(const std::string &name)
{
    return new DataStack(name);
}

//! Binds the set and get functions for the appropriate type.
template<typename T>
void bindSetGetVar(asIScriptEngine *engine, const std::string &asType)
{
    // Forward declarations.
    int r;
    std::stringstream ss;

    // Binds DataStack::setVar<T>
    ss.str("");
    ss << "bool set_" << asType << "(const string &in, const " << asType << ")";
    r = engine->RegisterObjectMethod("DataStack", ss.str().c_str(),
                                     asMETHODPR(DataStack, setVar<T>, (const std::string&, const T&), bool),
                                     asCALL_THISCALL);

    // Binds DataStack::getVar<T>
    ss.str("");
    ss << asType << " get_" << asType << "(const string &in)";
    r = engine->RegisterObjectMethod("DataStack", ss.str().c_str(),
                                     asMETHODPR(DataStack, getVar<T>, (const std::string&), T),
                                     asCALL_THISCALL); assert(r >= 0);
}

//! Binds the DataStack class to AngelScript.
void bindDataStack(asIScriptEngine *engine)
{
    // Forward declarations.
    int r;

    // Bind DataStack class.
    r = engine->RegisterObjectType("DataStack", sizeof(DataStack), asOBJ_REF); assert(r >= 0);

    // Set DataStack behaviour.
    r = engine->RegisterObjectBehaviour("DataStack", asBEHAVE_FACTORY, "DataStack@ f(const string &in)",
                                        asFUNCTION(createDataStack), asCALL_CDECL); assert(r >= 0);
    r = engine->RegisterObjectBehaviour("DataStack", asBEHAVE_ADDREF, "void f()",
                                        asMETHOD(DataStack, grab), asCALL_THISCALL); assert(r >= 0);
    r = engine->RegisterObjectBehaviour("DataStack", asBEHAVE_RELEASE, "void f()",
                                        asMETHOD(DataStack, drop), asCALL_THISCALL); assert(r >= 0);

    r = engine->RegisterObjectMethod("DataStack", "DataStack& opAssign(const DataStack &in)",
                                     asFUNCTION(assignT<DataStack>), asCALL_CDECL_OBJFIRST); assert(r >= 0);

    // Bind DataStack class functions.
    r = engine->RegisterObjectMethod("DataStack", "void init()",
                                     asMETHOD(DataStack, init), asCALL_THISCALL); assert(r >= 0);
    r = engine->RegisterObjectMethod("DataStack", "void clear()",
                                     asMETHOD(DataStack, clear), asCALL_THISCALL); assert(r >= 0);

    r = engine->RegisterObjectMethod("DataStack", "u32 getID()",
                                     asMETHOD(DataStack, getID), asCALL_THISCALL); assert(r >= 0);
    r = engine->RegisterObjectMethod("DataStack", "const string& getName()",
                                     asMETHOD(DataStack, getName), asCALL_THISCALL); assert(r >= 0);

    // Bind DataStack::setVar<T> and DataStack::getVar<T>
    bindSetGetVar<c8>(engine, "c8");
    bindSetGetVar<f32>(engine, "f32");
    bindSetGetVar<f64>(engine, "f64");
    bindSetGetVar<s16>(engine, "s16");
    bindSetGetVar<s32>(engine, "s32");
    bindSetGetVar<u16>(engine, "u16");
    bindSetGetVar<u32>(engine, "u32");
    bindSetGetVar<u8>(engine, "u8");
    bindSetGetVar<std::string>(engine, "string");

    r = engine->RegisterObjectMethod("DataStack", "void removeVars()",
                                     asMETHOD(DataStack, removeVars), asCALL_THISCALL); assert(r >= 0);
    r = engine->RegisterObjectMethod("DataStack", "bool removeVar(const string &in)",
                                     asMETHOD(DataStack, removeVar), asCALL_THISCALL); assert(r >= 0);

    r = engine->RegisterObjectMethod("DataStack", "bool saveBencode(const string &in)",
                                     asMETHOD(DataStack, saveBencode), asCALL_THISCALL);assert(r >= 0);
    r = engine->RegisterObjectMethod("DataStack", "bool loadBencode(const string &in)",
                                     asMETHOD(DataStack, loadBencode), asCALL_THISCALL);assert(r >= 0);

    r = engine->RegisterObjectMethod("DataStack", "bool saveXML(const string &in)",
                                     asMETHOD(DataStack, saveXML), asCALL_THISCALL);assert(r >= 0);
    r = engine->RegisterObjectMethod("DataStack", "bool loadXML(const string &in)",
                                     asMETHOD(DataStack, loadXML), asCALL_THISCALL);assert(r >= 0);
}

#endif // __COMPILE_WITH_ANGELSCRIPT__

// End of File
